﻿Imports Negocio
Imports Servicios
Imports Excepciones

Public Class frmListaRecurso

    Private _recursos As New List(Of Recurso)

    Protected Overrides Sub PreLoad()
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.Person.GetHicon())
    End Sub

    Sub New(recursos As List(Of Recurso), modulo As String)
        InitializeComponent()
        _recursos = recursos
        Me.Tag = modulo

    End Sub
    Private Sub frmListaRecurso_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Init()
    End Sub

    Sub Init()
        Try
            Dim ActiveRecursos As New List(Of Recurso)
            For Each obj As Recurso In _recursos
                If obj.Persistencia <> EstadoPersistencia.DELETE Then
                    ActiveRecursos.Add(obj)
                End If
            Next

            dgvRecursos.DataSource = ActiveRecursos


        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCerrar.Click
        Me.Close()
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnQuitar_Click(sender As System.Object, e As System.EventArgs) Handles btnQuitar.Click
        If dgvRecursos.Rows.Count > 0 AndAlso TypeOf dgvRecursos.CurrentRow.DataBoundItem Is Recurso Then
            If MessageExt.Show(GesIdiomas.TraduceMensaje("MSGRE002").ToString, MessageBoxButtons.OKCancel) = DialogResult.OK Then
                Dim rec As Recurso = DirectCast(dgvRecursos.CurrentRow.DataBoundItem, Recurso)
                If rec.Lock Then
                    MessageExt.Show(GesIdiomas.TraduceMensaje("MSGRE003").ToString)
                Else
                    rec.Persistencia = EstadoPersistencia.DELETE
                    Init()
                End If
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnAgregar_Click(sender As System.Object, e As System.EventArgs) Handles btnAgregar.Click
        Dim f = New frmConsultaRecurso(_recursos)
        If f.ShowDialog = DialogResult.OK Then
            Init()
        End If
    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        Me.Close()
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class